/*
 * ble_srvices.h
 *
 *  Created on: 2018年5月5日
 *      Author: 0yfj0
 */

#ifndef _BLE_SWITCH_H_
#define _BLE_SWITCH_H_
#ifdef __cplusplus
extern "C"
{
#endif  /* __cplusplus */
#include <stdint.h>
#include "ble.h"

/*----------------------------------------------------------------------------*/
#define BLE_CONN_AUTH ( 0x55AA55AA )
#define BLE_AUTH_KEY ( 0x1234AA55+'I'+'L'+'I'+'V'+'E' )
#define BLE_T1S_DENY ( 0 )
#define BLE_T1S_AUTH ( 0 )

/*----------------------------------------------------------------------------*/
void ble_lamp_services_init(void);
void ble_lamp_on_ble_evt(ble_evt_t *p_ble_evt);
void ble_current_time_update(void);
void ble_temperature_update(void);
void ble_humidity_update(void);
void ble_nus_send(void *buf, uint16_t len);

/*----------------------------------------------------------------------------*/
#define LAMP_UUID_BASE        {0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15, \
                              0xDE, 0xEF, 0x12, 0x12, 0x00, 0x00, 0x00, 0x00}
#define LAMP_UUID_SERVICE     0x1523
#define LAMP_UUID_LAMP_CHAR   0x1525

/*
 * Date UTC 	org.bluetooth.characteristic.date_utc 	0x2AED 	GSS
 * uint24
 *
 * Temperature 	org.bluetooth.characteristic.temperature 	0x2A6E 	GSS
 * sint16 Exponent: Decimal, -2
 *
 * Humidity 	org.bluetooth.characteristic.humidity 	0x2A6F 	GSS
 * uint16 Exponent: Decimal, -2
 * */
#define BLE_UUID_SERVICE_ENV_SENSORS	( 0x181A )
#define BLE_UUID_SERVICE_AUTOMATION_IO	( 0x1815 )
#define BLE_UUID_TEMPERATURE	( 0x2A6E )
#define BLE_UUID_HUMIDITY		( 0x2A6F )
#define BLE_UUID_DIGITAL		( 0x2A56 )
#define BLE_UUID_ANALOG			( 0x2A58 )

/*----------------------------------------------------------------------------*/
typedef struct ble_lamp_s ble_lamp_t;
typedef void (*ble_lamp_write_handler_t)(ble_lamp_t *p_lamp, int lamp,
		uint8_t new_state);
struct ble_lamp_s
{
	uint16_t service_handle; /**< Handle of LED Button Service (as provided by the BLE stack). */
//	uint16_t conn_handle; /**< Handle of the current connection (as provided by the BLE stack). BLE_CONN_HANDLE_INVALID if not in a connection. */
	ble_gatts_char_handles_t lamp1_char_handles; /**< Handles related to the LED Characteristic. */
	ble_gatts_char_handles_t lamp2_char_handles; /**< Handles related to the LED Characteristic. */
	ble_gatts_char_handles_t lamp3_char_handles; /**< Handles related to the LED Characteristic. */
	ble_lamp_write_handler_t lamp_write_handler; /**< Event handler to be called when the LED Characteristic is written. */
};
typedef struct
{
	ble_lamp_write_handler_t lamp_write_handler; /**< Event handler to be called when the LED Characteristic is written. */
} ble_lamp_init_t;
void ble_lamp_on_ble_evt(ble_evt_t *p_ble_evt);
void ble_lamp_lamp_on_ble_evt(ble_lamp_t *p_lamp, ble_evt_t *p_ble_evt);
/*----------------------------------------------------------------------------*/
typedef struct
{
	uint16_t service_handle;
	ble_gatts_char_handles_t temp_char_handles;
	ble_gatts_char_handles_t hum_char_handles;
} ble_env_sensors_t;
typedef struct ble_curr_tim_s ble_curr_tim_t;
typedef void (*ble_curr_tim_write_handler_t)(ble_curr_tim_t *p_curr_tim,
		uint8_t *p_data, uint16_t length);
struct ble_curr_tim_s
{
	uint16_t service_handle;
	ble_gatts_char_handles_t curr_tim_char_handles;
};
void ble_curr_tim_on_ble_evt(ble_curr_tim_t *p_curr_tim, ble_evt_t *p_ble_evt);

/*----------------------------------------------------------------------------*/
void ble_timer1s_task(void);

/*----------------------------------------------------------------------------*/

#ifdef __cplusplus
}
#endif  /* __cplusplus */
#endif /* _BLE_SWITCH_H_ */
